package cn.iocoder.yudao.module.team.service.ledger;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.team.controller.admin.ledger.vo.*;
import cn.iocoder.yudao.module.team.dal.dataobject.ledger.LedgerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.team.dal.mysql.ledger.LedgerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.team.enums.ErrorCodeConstants.*;

/**
 * 报账管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LedgerServiceImpl implements LedgerService {

    @Resource
    private LedgerMapper ledgerMapper;

    @Override
    public Integer createLedger(LedgerSaveReqVO createReqVO) {
        // 插入
        LedgerDO ledger = BeanUtils.toBean(createReqVO, LedgerDO.class);
        ledgerMapper.insert(ledger);

        // 返回
        return ledger.getId();
    }

    @Override
    public void updateLedger(LedgerSaveReqVO updateReqVO) {
        // 校验存在
        validateLedgerExists(updateReqVO.getId());
        // 更新
        LedgerDO updateObj = BeanUtils.toBean(updateReqVO, LedgerDO.class);
        ledgerMapper.updateById(updateObj);
    }

    @Override
    public void deleteLedger(Integer id) {
        // 校验存在
        validateLedgerExists(id);
        // 删除
        ledgerMapper.deleteById(id);
    }

    @Override
        public void deleteLedgerListByIds(List<Integer> ids) {
        // 删除
        ledgerMapper.deleteByIds(ids);
        }


    private void validateLedgerExists(Integer id) {
        if (ledgerMapper.selectById(id) == null) {
            throw exception(LEDGER_NOT_EXISTS);
        }
    }

    @Override
    public LedgerDO getLedger(Integer id) {
        return ledgerMapper.selectById(id);
    }

    @Override
    public PageResult<LedgerDO> getLedgerPage(LedgerPageReqVO pageReqVO) {
        return ledgerMapper.selectPage(pageReqVO);
    }

}